import { store } from '../index'
import { defineStore } from 'pinia'
import { getAccessToken, removeToken } from '@/utils/auth'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
import { getInfo, loginOut } from '@/api/login'

const { wsCache } = useCache()

interface UserVO {
  id: number
  avatar: string
  nickname: string
  deptId: number
  loginDeptId?: number
  loginRoleId?: number
  supAdmin?: boolean
  tenantAdmin?: boolean
  supAdminToken?: boolean
}

interface DicVO {
  id: number
  name: string
}
interface DeptVO {
  deptId: number
  deptName: string
  dutyInfoList: DicVO[]
  positionInfoList: DicVO[]
  postInfoList: DicVO[]
  roleInfoList: DicVO[]
}

interface UserInfoVO {
  permissions: object
  jeeLowCodepermissions: object
  roles: string[]
  isSetUser: boolean
  user: UserVO
  deptInfo: DeptVO[]
}

export const useUserStore = defineStore('admin-user', {
  state: (): UserInfoVO => ({
    permissions: {},
    jeeLowCodepermissions: {},
    roles: [],
    isSetUser: false,
    user: {
      id: 0,
      avatar: '',
      nickname: '',
      deptId: 0,
    },
    deptInfo: [],
  }),
  getters: {
    getPermissions(): object {
      return this.permissions
    },
    getJeeLowCodepermissions(): object {
      return this.jeeLowCodepermissions
    },
    getRoles(): string[] {
      return this.roles
    },
    getIsSetUser(): boolean {
      return this.isSetUser
    },
    getUser(): UserVO {
      return this.user
    }
  },
  actions: {
    async setUserInfoAction() {
      if (!getAccessToken()) {
        this.resetState()
        return null
      }
      let userInfo = wsCache.get(CACHE_KEY.USER)
      if (!userInfo) {
        userInfo = await getInfo()
      }
      this.permissions = userInfo.permissions
      this.jeeLowCodepermissions = userInfo.jeeLowCodepermissions
      this.roles = userInfo.roles
      this.user = userInfo.user
      this.deptInfo = userInfo.deptInfoList
      this.isSetUser = true
      wsCache.set(CACHE_KEY.USER, userInfo)
      wsCache.set(CACHE_KEY.ROLE_ROUTERS, userInfo.menus)
    },
    async setUserAvatarAction(avatar: string) {
      const userInfo = wsCache.get(CACHE_KEY.USER)
      // NOTE: 是否需要像`setUserInfoAction`一样判断`userInfo != null`
      this.user.avatar = avatar
      userInfo.user.avatar = avatar
      wsCache.set(CACHE_KEY.USER, userInfo)
    },
    async setUserNicknameAction(nickname: string) {
      const userInfo = wsCache.get(CACHE_KEY.USER)
      // NOTE: 是否需要像`setUserInfoAction`一样判断`userInfo != null`
      this.user.nickname = nickname
      userInfo.user.nickname = nickname
      wsCache.set(CACHE_KEY.USER, userInfo)
    },
    async loginOut() {
      await loginOut()
      removeToken()
      wsCache.clear()
      this.resetState()
    },
    resetState() {
      this.permissions = {}
      this.jeeLowCodepermissions = {}
      this.roles = []
      this.isSetUser = false
      this.user = {
        id: 0,
        avatar: '',
        nickname: '',
        deptId: 0
      }
    }
  },
})

export const useUserStoreWithOut = () => {
  return useUserStore(store)
}
